/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;
 
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Timer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO; 
import javax.swing.*;

/**
 *
 * @author theworldiwant
 */
public class Menu extends JPanel implements MouseListener, MouseMotionListener{
        private BufferedImage bg;
        private BufferedImage start;
        private Rectangle s;
        private boolean onTop;
       private String error;
       private  JTextField p, n ;
       private int pVal, nVal;
       private JFrame mainFrame;
       
        public Menu(JFrame frame){
              mainFrame=frame;
             this.setLayout(null);
             init();
            
            this.addMouseListener(this);
            this.addMouseMotionListener(this);
        }
        public void init(){
        try {
            bg = ImageIO.read(new File("images/2.png"));
            start = ImageIO.read(new File("images/start.png"));
            
        } catch (IOException ex) {
            Logger.getLogger(Menu.class.getName()).log(Level.SEVERE, null, ex);
        }
        s= new Rectangle();
        s.x=290;
        s.y=219; 
        s.width = 378;
        s.height = 278; 
        
        onTop=false;                                                //for the start button pra magiba ng color
        error="";
        
        n = new JTextField("");
        p= new JTextField("");
        
        n.addFocusListener(
                new FocusListener(){

            @Override
            public void focusGained(FocusEvent fe) { }

            @Override
            public void focusLost(FocusEvent fe) {
                error="";
                if(!n.getText().equals("")){
                         try{
                                nVal=Integer.parseInt(n.getText());
                                if(nVal<1)
                                    throw new Exception(); 
                                
                            }catch(Exception e){
                                error = "*Error: n must be a no.>0."; 
                                nVal=0;
                           }
                }
           }  
         }
        ); 
        
        p.addFocusListener(
                new FocusListener(){

            @Override
            public void focusGained(FocusEvent fe) {
              //  throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public void focusLost(FocusEvent fe) {
                error="";
                 if(!p.getText().equals("")){
                         try{
                             
                                pVal=Integer.parseInt(p.getText());
                                if(pVal<1)
                                    throw new Exception(); 
                            }catch(Exception e){
                                error = "*Error: p must be a no. >0.";
                                pVal=0;
                            }
                 }
            }
                    
                }
        );
        
        n.setBackground(Color.lightGray);
        p.setBackground(Color.lightGray);
        
        n.setBounds(175, 363, 138, 23); 
        p.setBounds(384, 364, 51, 23);
        
        this.add(n);
        this.add(p);
        }
        
        
    @Override
        public void paint(Graphics g){
            super.paint(g);
            Graphics2D g2D= (Graphics2D)g;
            g2D.drawImage(bg, 0, 0, null); 
       
            Font font = new Font("Cambria", Font.PLAIN, 14);
            g2D.setColor(Color.RED);
            g2D.setFont(font);
            
            g2D.drawString(error, 83,20);
           // g2D.drawImage(icon.getImage(), 274, 203, null);
            if(onTop)
                g2D.drawImage(start, 274, 203, null);
        }

    @Override
    public void mouseClicked(MouseEvent me) { 
       
            if(me.getX()>s.x&&me.getX()<s.width&&me.getY()>s.y&&me.getY()<s.height){ 
                         if(!n.getText().equals("")){
                            try{
                                nVal=Integer.parseInt(n.getText());
                                if(nVal<1)
                                    throw new Exception(); 
                            }catch(Exception e){
                                error = "*Error: n must be a no.>0."; 
                                nVal=0;
                           }
                         }
                         
                         if(!p.getText().equals("")){
                                try{
                                    pVal=Integer.parseInt(p.getText());
                                    if(pVal<1 || pVal>100)
                                        throw new Exception(); 
                                }catch(Exception e){
                                    error = "*Error: p must be a no. >0 and <= 100";
                                    pVal=0;
                                }
                        }
                       
                         if(n.getText().equals("")&&p.getText().equals(""))
                            error="*Error: n & p must have a value.";
                      
                        if( nVal>0&& pVal>0){                                                                   //for next GUI
                            error="";
                            mainFrame.setVisible(false);
                            Lucky9 game = new Lucky9(pVal, nVal);
                            Gui2 g = new Gui2(game.getFinalTable(), pVal, nVal, game.getRoundTable(), mainFrame, game.getDeckSize());
                            
                        }
            }
   }

    @Override
    public void mousePressed(MouseEvent me) {}

    @Override
    public void mouseReleased(MouseEvent me) {}

    @Override
    public void mouseEntered(MouseEvent me) {}

    @Override
    public void mouseExited(MouseEvent me) {}

    @Override
    public void mouseDragged(MouseEvent me) {}

    @Override
    public void mouseMoved(MouseEvent me) {
            if(me.getX()>s.x&&me.getX()<s.width&&me.getY()>s.y&&me.getY()<s.height){
                onTop=true;
                repaint();
            }
            else{
                onTop=false; 
                repaint();
            }
    }

 
}
